home *** CD-ROM | disk | FTP | other *** search
Text File | 1991-10-20 | 37.2 KB | 1,330 lines |
- /* This file has been changed-- it is not the standard FSF version.
-
- Oct 1991. */
-
-
- /* grep - print lines matching an extended regular expression
- Copyright (C) 1988 Free Software Foundation, Inc.
- Written June, 1988 by Mike Haertel
- BMG speedups added July, 1988
- by James A. Woods and Arthur David Olson
-
- NO WARRANTY
-
- BECAUSE THIS PROGRAM IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
- NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW. EXCEPT
- WHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC,
- RICHARD M. STALLMAN AND/OR OTHER PARTIES PROVIDE THIS PROGRAM "AS IS"
- WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
- BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
- FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY
- AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE
- DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
- CORRECTION.
-
- IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
- STALLMAN, THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTY
- WHO MAY MODIFY AND REDISTRIBUTE THIS PROGRAM AS PERMITTED BELOW, BE
- LIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR
- OTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
- USE OR INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR
- DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR
- A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) THIS
- PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
- DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
-
- GENERAL PUBLIC LICENSE TO COPY
-
- 1. You may copy and distribute verbatim copies of this source file
- as you receive it, in any medium, provided that you conspicuously and
- appropriately publish on each copy a valid copyright notice "Copyright
- (C) 1988 Free Software Foundation, Inc."; and include following the
- copyright notice a verbatim copy of the above disclaimer of warranty
- and of this License. You may charge a distribution fee for the
- physical act of transferring a copy.
-
- 2. You may modify your copy or copies of this source file or
- any portion of it, and copy and distribute such modifications under
- the terms of Paragraph 1 above, provided that you also do the following:
-
- a) cause the modified files to carry prominent notices stating
- that you changed the files and the date of any change; and
-
- b) cause the whole of any work that you distribute or publish,
- that in whole or in part contains or is a derivative of this
- program or any part thereof, to be licensed at no charge to all
- third parties on terms identical to those contained in this
- License Agreement (except that you may choose to grant more extensive
- warranty protection to some or all third parties, at your option).
-
- c) You may charge a distribution fee for the physical act of
- transferring a copy, and you may at your option offer warranty
- protection in exchange for a fee.
-
- Mere aggregation of another unrelated program with this program (or its
- derivative) on a volume of a storage or distribution medium does not bring
- the other program under the scope of these terms.
-
- 3. You may copy and distribute this program or any portion of it in
- compiled, executable or object code form under the terms of Paragraphs
- 1 and 2 above provided that you do the following:
-
- a) accompany it with the complete corresponding machine-readable
- source code, which must be distributed under the terms of
- Paragraphs 1 and 2 above; or,
-
- b) accompany it with a written offer, valid for at least three
- years, to give any third party free (except for a nominal
- shipping charge) a complete machine-readable copy of the
- corresponding source code, to be distributed under the terms of
- Paragraphs 1 and 2 above; or,
-
- c) accompany it with the information you received as to where the
- corresponding source code may be obtained. (This alternative is
- allowed only for noncommercial distribution and only if you
- received the program in object code or executable form alone.)
-
- For an executable file, complete source code means all the source code for
- all modules it contains; but, as a special exception, it need not include
- source code for modules which are standard libraries that accompany the
- operating system on which the executable file runs.
-
- 4. You may not copy, sublicense, distribute or transfer this program
- except as expressly provided under this License Agreement. Any attempt
- otherwise to copy, sublicense, distribute or transfer this program is void and
- your rights to use the program under this License agreement shall be
- automatically terminated. However, parties who have received computer
- software programs from you with this License Agreement will not have
- their licenses terminated so long as such parties remain in full compliance.
-
- 5. If you wish to incorporate parts of this program into other free
- programs whose distribution conditions are different, write to the Free
- Software Foundation at 675 Mass Ave, Cambridge, MA 02139. We have not yet
- worked out a simple rule that can be stated here, but we will often permit
- this. We will be guided by the two goals of preserving the free status of
- all derivatives our free software and of promoting the sharing and reuse of
- software.
-
-
- In other words, you are welcome to use, share and improve this program.
- You are forbidden to forbid anyone else to use, share and improve
- what you give them. Help stamp out software-hoarding! */
-
- #include <ctype.h>
- #include <stdio.h>
-
- #include <string.h>
- #include <stdlib.h>
- #include "dfa.h"
- #include "regex.h"
- #include "common.h"
-
- extern errno;
- extern char *sys_errlist[];
-
- #define MAX(a, b) ((a) > (b) ? (a) : (b))
-
- /* Exit status codes. */
- #define MATCHES_FOUND 0 /* Exit 0 if no errors and matches found. */
- #define NO_MATCHES_FOUND 1 /* Exit 1 if no matches were found. */
- #define ERROR 2 /* Exit 2 if some error occurred. */
-
- /* Error is set true if something awful happened. */
- static int error;
-
- /* The program name for error messages. */
- static char *prog;
- void CheckForBreak( void );
-
- /* We do all our own buffering by hand for efficiency. */
- static char *buffer; /* The buffer itself, grown as needed. */
- static bufbytes; /* Number of bytes in the buffer. */
- static size_t bufalloc; /* Number of bytes allocated to the buffer. */
- static bufprev; /* Number of bytes that have been forgotten.
- This is used to get byte offsets from the
- beginning of the file. */
- static bufread; /* Number of bytes to get with each read(). */
-
- short WeAreStopped;
-
- static void
- initialize_buffer()
- {
- bufread = 8192;
- bufalloc = bufread + bufread / 2;
- buffer = malloc(bufalloc);
- if (! buffer)
- {
- emitdata( "%s: Memory exhausted (%s)\r", prog,
- "Some error");
- exit(ERROR);
- }
- }
-
- /* The current input file. */
- static fd;
- static char *filename;
- static eof;
-
- /* Fill the buffer retaining the last n bytes at the beginning of the
- newly filled buffer (for backward context). Returns the number of new
- bytes read from disk. */
- static
- fill_buffer_retaining(n)
- int n;
- {
- char *p, *q;
- int i;
-
- /* See if we need to grow the buffer. */
- if (bufalloc - n <= bufread)
- {
- while (bufalloc - n <= bufread)
- {
- bufalloc *= 2;
- bufread *= 2;
- }
- buffer = realloc(buffer, bufalloc);
- if (! buffer)
- {
- emitdata( "%s: Memory exhausted (%s)\r", prog,
- "Some error");
- exit(ERROR);
- }
- }
-
- bufprev += bufbytes - n;
-
- /* Shift stuff down. */
- for (i = n, p = buffer, q = p + bufbytes - n; i--; )
- *p++ = *q++;
- bufbytes = n;
-
- if (eof)
- return 0;
-
- /* Read in new stuff. */
- i = read(fd, buffer + bufbytes, bufread);
- CheckForBreak();
- if (WeAreStopped) i = 0; /* exit this thing */
- if (i < 0)
- {
- emitdata( "%s: read on %s failed (%s)\r", prog,
- filename ? filename : "<stdin>", "Some error");
- error = 1;
- }
-
- /* Kludge to pretend every nonempty file ends with a newline. */
- if (i == 0 && bufbytes > 0 && buffer[bufbytes - 1] != '\r') /* newline changed */
- {
- eof = i = 1;
- buffer[bufbytes] = '\r'; /* newline changed */
- }
-
- bufbytes += i;
- return i;
- }
-
- /* Various flags set according to the argument switches. */
- static trailing_context = 0;/* Lines of context to show after matches. */
- static leading_context = 0; /* Lines of context to show before matches. */
- static byte_count = 0; /* Precede output lines the byte count of the
- first character on the line. */
- static no_filenames = 0; /* Do not display filenames. */
- static line_numbers = 0; /* Precede output lines with line numbers. */
- static silent = 0; /* Produce no output at all. This switch
- is bogus, ever hear of /dev/null? */
- static nonmatching_lines = 0; /* Print lines that don't match the regexp. */
-
- static bmgexec; /* Invoke Boyer-Moore-Gosper routines */
-
- /* The compiled regular expression lives here. */
- static struct regexp reg;
-
- /* The compiled regular expression for the backtracking matcher lives here. */
- static struct re_pattern_buffer regex;
-
- /* Pointer in the buffer after the last character printed. */
- static char *printed_limit;
-
- /* True when printed_limit has been artifically advanced without printing
- anything. */
- static int printed_limit_fake;
-
- static char prbuf[82];
- static prbufind = 0;
-
- /* Print a line at the given line number, returning the number of
- characters actually printed. Matching is true if the line is to
- be considered a "matching line". This is only meaningful if
- surrounding context is turned on. */
- static
- print_line(p, number, matching)
- char *p;
- int number;
- int matching;
- {
- int count = 0;
-
- if (silent)
- {
- do
- ++count;
- while (*p++ != '\r'); /* newline changed */
- printed_limit_fake = 0;
- printed_limit = p;
- return count;
- }
-
- if (filename && !no_filenames)
- emitdata("%s%c", filename, matching ? ':' : '-');
- if (byte_count)
- emitdata("%d%c", p - buffer + bufprev, matching ? ':' : '-');
- if (line_numbers)
- emitdata("%d%c", number, matching ? ':' : '-');
- do
- {
- ++count;
- if (*p == '\r') {
- prbuf[prbufind] = '\r';
- prbufind++;
- prbuf[prbufind] = '\0';
- emitdata("%s", prbuf);
- prbufind = 0;
- } else {
- prbuf[prbufind] = *p;
- prbufind++;
- if (prbufind == 80) {
- prbuf[prbufind] = '\0';
- emitdata("%s", prbuf);
- prbufind = 0;
- }
- }
- }
- while (*p++ != '\r'); /* newline changed */
- printed_limit_fake = 0;
- printed_limit = p;
- return count;
- }
-
- /* Print matching or nonmatching lines from the current file. Returns a
- count of matching or nonmatching lines. */
- static
- grep()
- {
- int retain = 0; /* Number of bytes to retain on next call
- to fill_buffer_retaining(). */
- char *search_limit; /* Pointer to the character after the last
- newline in the buffer. */
- char saved_char; /* Character after the last newline. */
- char *resume; /* Pointer to where to resume search. */
- int resume_index = 0; /* Count of characters to ignore after
- refilling the buffer. */
- int line_count = 1; /* Line number. */
- int try_backref; /* Set to true if we need to verify the
- match with a backtracking matcher. */
- int initial_line_count; /* Line count at beginning of last search. */
- char *match; /* Pointer to the first character after the
- string matching the regexp. */
- int match_count = 0; /* Count of matching lines. */
- char *matching_line; /* Pointer to first character of the matching
- line, or of the first line of context to
- print if context is turned on. */
- char *real_matching_line; /* Pointer to the first character of the
- real matching line. */
- char *next_line; /* Pointer to first character of the line
- following the matching line. */
- int pending_lines = 0; /* Lines of context left over from last match
- that we have to print. */
- static first_match = 1; /* True when nothing has been printed. */
- int i;
- char *tmp;
- char *execute();
-
- printed_limit_fake = 0;
-
- while (fill_buffer_retaining(retain) > 0)
- {
- /* Find the last newline in the buffer. */
- search_limit = buffer + bufbytes;
- while (search_limit > buffer && search_limit[-1] != '\r') /* newline changed */
- --search_limit;
- if (search_limit == buffer)
- {
- retain = bufbytes;
- continue;
- }
-
- /* Save the character after the last newline so regexecute can write
- its own sentinel newline. */
- saved_char = *search_limit;
-
- /* Search the buffer for a match. */
- printed_limit = buffer;
- resume = buffer + resume_index;
- initial_line_count = line_count;
-
- while (match = execute(®, resume, search_limit, 0, &line_count, &try_backref))
- {
- ++match_count;
-
- /* Find the beginning of the matching line. */
- matching_line = match;
- while (matching_line > resume && matching_line[-1] != '\r') /* newline changed */
- --matching_line;
- real_matching_line = matching_line;
-
- /* Find the beginning of the next line. */
- next_line = match;
- while (next_line < search_limit && *next_line++ != '\r') /* newline changed */
- ;
-
- /* If a potential backreference is indicated, try it out with
- a backtracking matcher to make sure the line is a match. */
- if (try_backref && re_search(®ex, matching_line,
- next_line - matching_line - 1,
- 0,
- next_line - matching_line - 1,
- NULL) < 0)
- {
- resume = next_line;
- if (resume == search_limit)
- break;
- else
- continue;
- }
-
- /* Print leftover lines from last time. If nonmatching_lines is
- turned on, print these as if they were matching lines. */
- while (resume < matching_line && pending_lines)
- {
- resume += print_line(resume, initial_line_count++,
- nonmatching_lines);
- --pending_lines;
- }
-
- /* Print out the matching or nonmatching lines as necessary. */
- if (! nonmatching_lines)
- {
- /* Back up over leading context if necessary. */
- for (i = leading_context; matching_line > printed_limit
- && i; --i)
- {
- while (matching_line > printed_limit
- && (--matching_line)[-1] != '\r') /* newline changed */
- ;
- --line_count;
- }
-
- /* If context is enabled, we may have to print a separator. */
- if ((leading_context || trailing_context) && !silent
- && !first_match && (printed_limit_fake || matching_line
- > printed_limit))
- emitdata("----------\r");
- first_match = 0;
-
- /* Print the matching line and its leading context. */
- while (matching_line < real_matching_line)
- matching_line += print_line(matching_line, line_count++, 0);
- matching_line += print_line(matching_line, line_count++, 1);
-
- /* If there's trailing context, leave some lines pending until
- next time. */
- pending_lines = trailing_context;
- }
- else if (matching_line > resume)
- {
- char *real_resume = resume;
-
- /* Back up over leading context if necessary. */
- for (i = leading_context; resume > printed_limit && i; --i)
- {
- while (resume > printed_limit && (--resume)[-1] != '\r') /* newline changed */
- ;
- --initial_line_count;
- }
-
- /* If context is enabled, we may have to print a separator. */
- if ((leading_context || trailing_context) && !silent
- && !first_match && (printed_limit_fake || resume
- > printed_limit))
- emitdata("----------\r");
- first_match = 0;
-
- /* Print out the presumably matching leading context. */
- while (resume < real_resume)
- resume += print_line(resume, initial_line_count++, 0);
-
- /* Print out the nonmatching lines prior to the matching line. */
- while (resume < matching_line)
- resume += print_line(resume, initial_line_count++, 1);
-
- /* Deal with trailing context. */
- if (trailing_context)
- {
- print_line(matching_line, line_count, 0);
- pending_lines = trailing_context - 1;
- }
-
- /* Count the current line. */
- ++line_count;
- }
- else
- {
- /* The line immediately after a matching line has to be printed
- because it was pending. */
- if (pending_lines > 0)
- {
- --pending_lines;
- print_line(matching_line, line_count, 0);
- }
- ++line_count;
- }
-
- /* Resume searching at the beginning of the next line. */
- initial_line_count = line_count;
- resume = next_line;
-
- if (resume == search_limit)
- break;
- }
-
- /* Restore the saved character. */
- *search_limit = saved_char;
-
- if (! nonmatching_lines)
- {
- while (resume < search_limit && pending_lines)
- {
- resume += print_line(resume, initial_line_count++, 0);
- --pending_lines;
- }
- }
- else if (search_limit > resume)
- {
- char *initial_resume = resume;
-
- /* Back up over leading context if necessary. */
- for (i = leading_context; resume > printed_limit && i; --i)
- {
- while (resume > printed_limit && (--resume)[-1] != '\r') /* newline changed */
- ;
- --initial_line_count;
- }
-
- /* If context is enabled, we may have to print a separator. */
- if ((leading_context || trailing_context) && !silent
- && !first_match && (printed_limit_fake || resume
- > printed_limit))
- emitdata("----------\r");
- first_match = 0;
-
- /* Print out all the nonmatching lines up to the search limit. */
- while (resume < initial_resume)
- resume += print_line(resume, initial_line_count++, 0);
- while (resume < search_limit)
- resume += print_line(resume, initial_line_count++, 1);
-
- pending_lines = trailing_context;
- resume_index = 0;
- retain = bufbytes - (search_limit - buffer);
- continue;
- }
-
- /* Save the trailing end of the buffer for possible use as leading
- context in the future. */
- i = leading_context;
- tmp = search_limit;
- while (tmp > printed_limit && i--)
- while (tmp > printed_limit && (--tmp)[-1] != '\r') /* newline changed */
- ;
- resume_index = search_limit - tmp;
- retain = bufbytes - (tmp - buffer);
- if (tmp > printed_limit)
- printed_limit_fake = 1;
- }
-
- return nonmatching_lines ? (line_count - 1) - match_count : match_count;
- }
-
-
-
- /*
- bmg_setup() and bmg_search() adapted from:
- Boyer/Moore/Gosper-assisted 'egrep' search, with delta0 table as in
- original paper (CACM, October, 1977). No delta1 or delta2. According to
- experiment (Horspool, Soft. Prac. Exp., 1982), delta2 is of minimal
- practical value. However, to improve for worst case input, integrating
- the improved Galil strategies (Apostolico/Giancarlo, Siam. J. Comput.,
- February 1986) deserves consideration.
-
- James A. Woods Copyleft (C) 1986, 1988
- NASA Ames Research Center
- */
-
- char *
- execute(r, begin, end, newline, count, try_backref)
- struct regexp *r;
- char *begin;
- char *end;
- int newline;
- int *count;
- int *try_backref;
- {
- register char *p, *s;
- char *match;
- char *start = begin;
- char save; /* regexecute() sentinel */
- int len;
- char *bmg_search();
-
- if (!bmgexec) /* full automaton search */
- return(regexecute(r, begin, end, newline, count, try_backref));
- else
- {
- len = end - begin;
- while ((match = bmg_search((unsigned char *) start, len)) != NULL)
- {
- p = match; /* narrow search range to submatch line */
- while (p > begin && *p != '\r') /* newline changed */
- p--;
- s = match;
- while (s < end && *s != '\r') /* newline changed */
- s++;
- s++;
-
- save = *s;
- *s = '\0';
- match = regexecute(r, p, s, newline, count, try_backref);
- *s = save;
-
- if (match != NULL)
- return((char *) match);
- else
- {
- start = s;
- len = end - start;
- }
- }
- return(NULL);
- }
- }
-
- #include <ctype.h>
- int delta0[256];
- unsigned char cmap[256]; /* (un)folded characters */
- unsigned char pattern[5000];
- int patlen;
-
- char *
- bmg_search(buffer, buflen)
- unsigned char *buffer;
- int buflen;
- {
- register unsigned char *k, *strend, *s, *buflim;
- register int t;
- int j;
-
- if (patlen > buflen)
- return NULL;
-
- buflim = buffer + buflen;
- if (buflen > patlen * 4)
- strend = buflim - patlen * 4;
- else
- strend = buffer;
-
- s = buffer;
- k = buffer + patlen - 1;
-
- for (;;)
- {
- /* The dreaded inner loop, revisited. */
- while (k < strend && (t = delta0[*k]))
- {
- k += t;
- k += delta0[*k];
- k += delta0[*k];
- }
- while (k < buflim && delta0[*k])
- ++k;
- if (k == buflim)
- break;
-
- j = patlen - 1;
- s = k;
- while (--j >= 0 && cmap[*--s] == pattern[j])
- ;
- /*
- delta-less shortcut for literati, but
- short shrift for genetic engineers.
- */
- if (j >= 0)
- k++;
- else /* submatch */
- return ((char *)k);
- }
- return(NULL);
- }
-
- bmg_setup(pat, folded) /* compute "boyer-moore" delta table */
- char *pat;
- int folded;
- { /* ... HAKMEM lives ... */
- int j;
-
- patlen = strlen(pat);
-
- if (folded) /* fold case while saving pattern */
- for (j = 0; j < patlen; j++)
- pattern[j] = (isupper((int) pat[j]) ?
- (char) tolower((int) pat[j]) : pat[j]);
- else
- memcpy(pattern, pat, patlen);
-
- for (j = 0; j < 256; j++)
- {
- delta0[j] = patlen;
- cmap[j] = (char) j; /* could be done at compile time */
- }
- for (j = 0; j < patlen - 1; j++)
- delta0[pattern[j]] = patlen - j - 1;
- delta0[pattern[patlen - 1]] = 0;
-
- if (folded)
- {
- for (j = 0; j < patlen - 1; j++)
- if (islower((int) pattern[j]))
- delta0[toupper((int) pattern[j])] = patlen - j - 1;
- if (islower((int) pattern[patlen - 1]))
- delta0[toupper((int) pattern[patlen - 1])] = 0;
- for (j = 'A'; j <= 'Z'; j++)
- cmap[j] = (char) tolower((int) j);
- }
- }
-
-
- #include <stdarg.h>
- #include <ctype.h>
- #include <memory.h>
- #include <Menus.h>
- #include <SysEqu.h>
- #include <pascal.h>
- #include <Dialogs.h>
- #include <Menus.h>
- #include <OSUtils.h>
- #include <StandardFile.h>
- #include "TransSkel.h"
- #include "TransDisplay.h"
- #include "gff.h"
-
- extern void *alloca();
-
- #define FileMenuID 500
- #define LSTWIND 500
- #define AboutID 500
- #define MessageID 502
- #define ARGDLOG 503
- #define HELPID 504
- #define PFolderID 200
- #define ErrorID 501
- #define ARGSEARCH 1
- #define ARGCANCEL 2
- #define ARGPFOLDER 3
- #define ARGPFILE 4
- #define ARGHELP 5
- #define ARGREGEXP 8
- #define ARGFILEPAT 9
- #define ARGSHOWFNAME 10
- #define ARGIGNCASE 11
- #define ARGONLYFNAME 12
- #define ARGSHOWLNUM 13
- #define ARGSHSEARCH 14
- #define ARGNONMATCH 15
- #define ARGTRAILCONT 16
- #define ARGLEADCONT 18
- #define ARGSHOWBYTE 20
- #define ARGCOUNT 21
- #define ARGWHWORD 22
- #define ARGWHLINE 23
- #define ARGOUTFILE 24
-
- #define ON 1
- #define OFF 0
-
- static WindowPtr theWindow;
- static short AllDone = false;
- static DialogPtr ArgDialog;
- static int argc;
- static char **argv;
- static int ignore_case = 1; /* Compile the regexp to ignore case. */
- static char regexp_buf[256]; /* The regular expression. */
- static int regexp_len; /* Length of the regular expression. */
- static int count_lines = 0; /* Show only count of matching lines. */
- static int list_files = 0; /* Show only names of matching files. */
- static int whole_word = 0; /* regexp much match a word only. */
- static int whole_line = 0; /* matching only whole lines. */
- static int line_count = 0; /* Count of matching lines for a file. */
- static int use_outfile = 0; /* Output to file */
- static SFReply tmpFile;
- static int ShowSearchedFiles = 0; /* Display files searched */
- static char file_pattern[256];
- static char translate[_NOTCHAR]; /* Translate table for case conversion
- (needed by the backtracking matcher). */
- static SkelStarted = false;
- static WDPBRec theWDPBRec;
- static KeyMap km;
- static int OutFileOpen;
- static FILE *fp;
-
- void CheckForBreak( void )
- {
- int i;
-
- GetKeys(km);
- if (km[1] == 8421376L) /* Seems to be fan-period */
- WeAreStopped = true;
- }
-
-
- void AbEnd(Str255 p)
- {
- ParamText(p, "\p", "\p", "\p");
- Alert(ErrorID, 0L);
- if (OutFileOpen) {
- fclose(fp);
- OutFileOpen = false;
- }
- /* Close old WD if needed */
- if (theWDPBRec.ioWDProcID != 0) PBCloseWD(&theWDPBRec, false);
- if (SkelStarted) {
- SkelClobber();
- SkelWhoa();
- }
- exit(1);
- }
-
-
- void Message(Str255 p)
- {
- ParamText(p, "\p", "\p", "\p");
- Alert(MessageID, 0L);
- }
-
- static void SetArgItem(DialogPtr ArgDialog, short item, short value)
- {
- short itemType;
- Rect itemRect;
- Handle itemHandle;
-
- GetDItem(ArgDialog, item, &itemType, &itemHandle, &itemRect);
- SetCtlValue((ControlHandle) itemHandle, value);
- }
-
- pascal Boolean FoldersOnly(p)
- ParmBlkPtr p;
- {
- /* Normally, folders are ALWAYS shown, and aren't even passed to */
- /* this file filter for judgement. Under such circumstances, it is */
- /* only necessary to blindly return TRUE (allow no files whatsoever). */
- /* However, Standard File is not documented in such a manner, and */
- /* this feature may not be TRUE in the future. Therefore, we DO check */
- /* to see if the entry passed to us describes a file or a directory. */
-
- if ((p->fileParam.ioFlAttrib & 0x10) != 0)
- return(false);
- return(true);
- }
-
-
- static int GetSearchFolder(void)
- {
- SFReply theDir;
- Point p;
- char *fname;
- OSType ftype = 'TEXT'; /* Value does not matter */
- OSErr err;
-
- p.h = 100; p.v = 100;
- gff_Get(&p, "\pPick the search folder", FoldersOnly, 0, &ftype,
- &theDir, PFolderID);
-
- /* SFGetFile(p,"\p",0L,1,&ftype,0L,&theDir); */
-
- if (theDir.good) {
- /* Close old WD if needed */
- if (theWDPBRec.ioWDProcID != 0) {
- if ((err = PBCloseWD(&theWDPBRec, false)) != noErr)
- AbEnd("\pCould not close working directory!");
- }
- theWDPBRec.ioCompletion = 0L;
- theWDPBRec.ioNamePtr = 0L;
- theWDPBRec.ioVRefNum = theDir.vRefNum;
- theWDPBRec.ioWDProcID = 'gReP';
- theWDPBRec.ioWDDirID = theDir.fType;
- if ((err = PBOpenWD(&theWDPBRec, false)) != noErr) {
- AbEnd("\pCould not open working directory!");
- }
-
- SetVol(0L, theWDPBRec.ioVRefNum); /* Set default vol to wd */
- return true;
- } else
- return false;
- }
-
-
- /* Toggle value of checkbox. Return new value. */
- static short ToggleArgItem(DialogPtr ArgDialog, short item)
- {
- short itemType;
- Rect itemRect;
- Handle itemHandle;
- short value;
-
- GetDItem(ArgDialog, item, &itemType, &itemHandle, &itemRect);
- value = GetCtlValue((ControlHandle) itemHandle);
- value = 1 - value;
- SetCtlValue((ControlHandle) itemHandle, value);
- return value;
- }
-
-
- static void SetArgDlog(void)
- {
- short itemType;
- Rect itemRect;
- Handle itemHandle;
- char NumBuf[32];
-
- GetDItem(ArgDialog, ARGREGEXP, &itemType, &itemHandle, &itemRect);
- SetIText(itemHandle, CtoPstr(regexp_buf));
-
- GetDItem(ArgDialog, ARGFILEPAT, &itemType, &itemHandle, &itemRect);
- SetIText(itemHandle, CtoPstr(file_pattern));
-
- sprintf(NumBuf, "%d", leading_context);
- GetDItem(ArgDialog, ARGLEADCONT, &itemType, &itemHandle, &itemRect);
- SetIText(itemHandle, CtoPstr(NumBuf));
-
- sprintf(NumBuf, "%d", trailing_context);
- GetDItem(ArgDialog, ARGTRAILCONT, &itemType, &itemHandle, &itemRect);
- SetIText(itemHandle, CtoPstr(NumBuf));
-
- SetArgItem(ArgDialog, ARGSHOWFNAME, 1 - no_filenames);
- SetArgItem(ArgDialog, ARGIGNCASE, ignore_case);
- SetArgItem(ArgDialog, ARGONLYFNAME, list_files);
- SetArgItem(ArgDialog, ARGSHOWLNUM, line_numbers);
- SetArgItem(ArgDialog, ARGSHSEARCH, ShowSearchedFiles);
- SetArgItem(ArgDialog, ARGNONMATCH, nonmatching_lines);
- SetArgItem(ArgDialog, ARGSHOWBYTE, byte_count);
- SetArgItem(ArgDialog, ARGCOUNT, count_lines);
- SetArgItem(ArgDialog, ARGWHWORD, whole_word);
- SetArgItem(ArgDialog, ARGWHLINE, whole_line);
- SetArgItem(ArgDialog, ARGOUTFILE, use_outfile);
- }
-
-
- /* Check to see if the user's option choices are sane */
- int CheckChoices(DialogPtr ArgDialog)
- {
- short itemType;
- Rect itemRect;
- Handle itemHandle;
- void glob_filelist();
- char NumBuf[32];
-
- GetDItem(ArgDialog, ARGREGEXP, &itemType, &itemHandle, &itemRect);
- GetIText(itemHandle, (void *) regexp_buf);
- (void) PtoCstr((unsigned char *) regexp_buf);
- regexp_len = strlen(regexp_buf);
- if (regexp_len == 0) {
- Message("\pYou must enter a regexp!");
- return false;
- }
-
- GetDItem(ArgDialog, ARGFILEPAT, &itemType, &itemHandle, &itemRect);
- GetIText(itemHandle, (void *) file_pattern);
- (void) PtoCstr((unsigned char *) file_pattern);
- glob_filelist(file_pattern, &argc, &argv);
- if (argc == 0) {
- Message("\pFilename pattern matches nothing!");
- return false;
- }
-
- GetDItem(ArgDialog, ARGLEADCONT, &itemType, &itemHandle, &itemRect);
- GetIText(itemHandle, (void *) NumBuf);
- (void) PtoCstr((unsigned char *) NumBuf);
- if (sscanf(NumBuf, "%d", &leading_context) != 1) {
- Message("\pLeading context must be a non-negative integer");
- return false;
- }
-
- GetDItem(ArgDialog, ARGTRAILCONT, &itemType, &itemHandle, &itemRect);
- GetIText(itemHandle, (void *) NumBuf);
- (void) PtoCstr((unsigned char *) NumBuf);
- if (sscanf(NumBuf, "%d", &trailing_context) != 1) {
- Message("\pTrailing context must be a non-negative integer");
- return false;
- }
-
- if (leading_context < 0 || trailing_context < 0) {
- Message("\pContext lines must be a non-negative integer");
- return false;
- }
-
- return true;
- }
-
- static Point dlogWhere = { 70, 100 };
-
- static int GetOutFile (void)
- {
- FInfo fndrInfo; /* finder info */
-
- SFPutFile (dlogWhere, "\pSave file as:", "\puntitled", nil, &tmpFile);
- if (!tmpFile.good) {
- return false;
- }
-
- /* Delete if exists */
- if (GetFInfo (tmpFile.fName, tmpFile.vRefNum, &fndrInfo) == noErr)
- if (FSDelete(tmpFile.fName, tmpFile.vRefNum) != noErr) {
- Message("\pCannot Delete File");
- return false;
- }
-
-
- if (Create (tmpFile.fName,tmpFile.vRefNum,'ttxt','TEXT') != noErr) {
- Message("\pCan't Create File");
- return false;
- }
-
- return true;
-
- }
-
- short DoArgDlog (void)
- {
- short int itemHit, dialogDone = false;
- short itemType, Cancel = false;
- Rect itemRect;
- Handle itemHandle;
- int c;
-
- ArgDialog = GetNewDialog(ARGDLOG, 0L, (WindowPtr) -1);
- SetArgDlog();
-
- ShowWindow(ArgDialog);
-
- while (dialogDone == false) {
-
- ModalDialog(0L, &itemHit);
- switch (itemHit)
- {
- case ARGCANCEL:
- Cancel = true;
- dialogDone = true;
- (void) PtoCstr((unsigned char *) regexp_buf);
- (void) PtoCstr((unsigned char *) file_pattern);
- break;
- case ARGSEARCH:
- if (CheckChoices(ArgDialog)) dialogDone = true;
- break;
- case ARGPFOLDER:
- (void) GetSearchFolder();
- break;
- case ARGPFILE:
- Message("\pNot implemented, use file pattern");
- break;
- case ARGHELP:
- Alert(HELPID, 0L);
- break;
- case ARGSHOWFNAME:
- no_filenames = 1 - ToggleArgItem(ArgDialog, ARGSHOWFNAME);
- break;
- case ARGIGNCASE:
- ignore_case = ToggleArgItem(ArgDialog, ARGIGNCASE);
- break;
- case ARGONLYFNAME:
- list_files = ToggleArgItem(ArgDialog, ARGONLYFNAME);
- silent = list_files;
- break;
- case ARGSHOWLNUM:
- line_numbers = ToggleArgItem(ArgDialog, ARGSHOWLNUM);
- break;
- case ARGSHSEARCH:
- ShowSearchedFiles = ToggleArgItem(ArgDialog, ARGSHSEARCH);
- break;
- case ARGNONMATCH:
- nonmatching_lines = ToggleArgItem(ArgDialog, ARGNONMATCH);
- case ARGSHOWBYTE:
- byte_count = ToggleArgItem(ArgDialog, ARGSHOWBYTE);
- break;
- case ARGCOUNT:
- count_lines = ToggleArgItem(ArgDialog, ARGCOUNT);
- silent = count_lines;
- break;
- case ARGWHWORD:
- whole_word = ToggleArgItem(ArgDialog, ARGWHWORD);
- break;
- case ARGWHLINE:
- whole_line = ToggleArgItem(ArgDialog, ARGWHLINE);
- break;
- case ARGOUTFILE:
- use_outfile = ToggleArgItem(ArgDialog, ARGOUTFILE);
- break;
- } /* switch */
-
- } /* while */
-
- HideWindow(ArgDialog);
- DisposDialog (ArgDialog);
-
- if (Cancel) return false;
-
- if (ignore_case) {
- for (c = 0; c < _NOTCHAR; c++)
- if (isupper(c))
- translate[c] = tolower(c);
- else
- translate[c] = c;
- regex.translate = translate;
- }
-
- return true;
-
- } /* DoArgDlog */
-
-
- static char edatabuf[256];
-
- void emitdata(char *pat, ...)
- {
- va_list ap;
-
- va_start(ap, pat);
- vsprintf(edatabuf, pat, ap);
- if (OutFileOpen)
- fprintf(fp, "%s", edatabuf);
- else
- DisplayText(edatabuf, strlen(edatabuf));
- va_end(ap);
- }
-
-
- static void DoAbout(void)
- {
- Alert(AboutID, 0L);
- }
-
-
- /* Needed by the regexp routines. This could be fancier, especially when
- dealing with parallel regexps in files. */
- void
- regerror(s)
- char *s;
- {
- AbEnd((void *)CtoPstr((void *)s));
- }
-
-
-
- void ExecuteGrep(void)
- {
- char *the_regexp;
- int i;
-
- int matches_found = 0; /* True if matches were found. */
- char *regex_errmesg; /* Error message from regex routines. */
-
- WeAreStopped = false;
-
- if (argc == 1) no_filenames = 1;
-
- the_regexp = regexp_buf;
-
- /* Set the syntax depending on whether we are EGREP or not. */
- #ifdef EGREP
- regsyntax(RE_SYNTAX_EGREP, ignore_case);
- re_set_syntax(RE_SYNTAX_EGREP);
- #else
- regsyntax(RE_SYNTAX_GREP, ignore_case);
- re_set_syntax(RE_SYNTAX_GREP);
- #endif
-
- if (regexp_len == 0) emitdata("Error: no regexp!\r"); /* should die! */
-
- if (whole_word || whole_line)
- {
- char *n = malloc(regexp_len + 8);
- int i = 0;
-
- if (whole_line)
- n[i++] = '^';
- else
- n[i++] = '\\', n[i++] = '<';
- #ifndef EGREP
- n[i++] = '\\';
- #endif
- n[i++] = '(';
- memcpy(n + i, the_regexp, regexp_len);
- i += regexp_len;
- #ifndef EGREP
- n[i++] = '\\';
- #endif
- n[i++] = ')';
- if (whole_line)
- n[i++] = '$';
- else
- n[i++] = '\\', n[i++] = '>';
- the_regexp = n;
- regexp_len = i;
- }
-
- regcompile(the_regexp, regexp_len, ®, 1);
-
- if (regex_errmesg = re_compile_pattern(the_regexp, regexp_len, ®ex)) {
- Message("\pSyntax error in regular expression");
- goto GiveUp;
- }
-
- /*
- Find the longest metacharacter-free string which must occur in the
- regexpr, before short-circuiting regexecute() with Boyer-Moore-Gosper.
- (Conjecture: The problem in general is NP-complete.) If there is no
- such string (like for many alternations), then default to full automaton
- search. regmust() code and heuristics [see dfa.c] courtesy
- Arthur David Olson.
- */
- if (line_numbers == 0 && nonmatching_lines == 0)
- {
- if (reg.mustn == 0 || reg.mustn == MUST_MAX ||
- strchr(reg.must, '\0') != reg.must + reg.mustn)
- bmgexec = 0;
- else
- {
- reg.must[reg.mustn] = '\0';
- if (getenv("MUSTDEBUG") != NULL)
- (void) emitdata("must have: \"%s\"\r", reg.must);
- bmg_setup(reg.must, ignore_case);
- bmgexec = 1;
- }
- }
-
- initialize_buffer();
-
- DisplayString("\p\r *** Press Command-period to stop the search ***\r\r");
-
- for (i=0; i<argc && !WeAreStopped; i++) {
- bufprev = eof = 0;
- filename = argv[i];
- if (ShowSearchedFiles) emitdata(" ... Searching %s\r", filename);
- fd = open(filename, 0, 0);
- if (fd < 0) {
- emitdata( "%s: %s: %s\r", prog, filename, "Could not open file");
- error = 1;
- continue;
- }
- if (line_count = grep()) matches_found = 1;
- close(fd);
- if (count_lines)
- if (!no_filenames)
- emitdata("%s:%d\r", filename, line_count);
- else
- emitdata("%d\r", line_count);
- else if (list_files && line_count)
- emitdata("%s\r", filename);
- } /* for */
-
- /* Cut down on the memory leaks */
- GiveUp:
- (void) alloca(0L);
- for (i=0; i<argc; i++) free(argv[i]);
- free(argv);
-
- }
-
-
- void DoGrep(void)
- {
- short OkToGo;
- short int tmpRefNum;
- unsigned char tmp[256];
- char *fname;
-
- OkToGo = DoArgDlog();
-
- if (OkToGo) {
- if (use_outfile) {
- if (!GetOutFile()) return;
- GetVol(tmp, &tmpRefNum);
- SetVol(0L, tmpFile.vRefNum);
- fname = PtoCstr(tmpFile.fName);
- if ((fp = fopen(fname, "a")) == NULL)
- AbEnd("\pCould not open output file");
- OutFileOpen = true;
- SetVol(tmp, tmpRefNum);
- DisplayString("\pWriting to output file");
- }
- ExecuteGrep();
- if (OutFileOpen) {
- fclose(fp);
- OutFileOpen = false;
- }
- }
- }
-
-
- static void DoFileMenu (Integer item)
- {
- OSErr err;
-
- switch (item)
- {
- case 1:
- DoGrep();
- break;
- case 3:
- /* Close old WD if needed */
- if (theWDPBRec.ioWDProcID != 0) {
- if ((err = PBCloseWD(&theWDPBRec, false)) != noErr)
- AbEnd("\pCould not close working directory!");
- }
- SkelRmveWind(theWindow);
- SkelWhoa (); /* tell SkelMain to quit */
- break;
- }
- }
-
-
-
- main()
- {
-
- int ensureStack=48000;
- MenuHandle MenuH;
-
- file_pattern[0] = '\0'; /* Init to null string */
- regexp_buf[0] = '\0';
-
- theWDPBRec.ioWDProcID = 0; /* If changes, we know WD has been opened */
-
- if (*(long *)CurStackBase - *(long *)ApplLimit < ensureStack)
- SetApplLimit((Ptr)(*(long *)CurStackBase - ensureStack));
- MaxApplZone();
-
- SkelInit (6, nil);
- SkelApple ("\pAbout egrep...", DoAbout);
-
- MenuH = GetMenu (FileMenuID);
- (void) SkelMenu (MenuH, DoFileMenu, nil, false);
-
- theWindow = GetNewDWindow(LSTWIND, nil);
-
- DoGrep();
-
- SkelStarted = true;
- SkelMain (); /* loop 'til Quit selected */
- SkelClobber (); /* clean up */
- }
-